//
//  PersonalSongsViewCtl.m
//  WYMusic
//
//  Created by ios on 16/9/20.
//  Copyright © 2016年 wy. All rights reserved.
//

#import "PersonalSongsViewCtl.h"
#import "SongListsViewCtl.h"
#import "NetworkRequest.h"
#import "UIImageView+WebCache.h"
#import "PlayViewCtl.h"
#import "MJRefresh.h"


#define SCREEN_WIDTH ( [[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ( [[UIScreen mainScreen] bounds].size.height)
#define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f \
blue:(b)/255.0f alpha:1.0f]


@interface PersonalSongsViewCtl ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *wTableView;
@property(nonatomic,strong)UIView *headerView;
@property(nonatomic,strong)UIImageView *headerImage;

@property(nonatomic,strong)NSMutableArray *songArray;
@property(nonatomic,strong)NSMutableArray *imageArray;
@property(nonatomic,strong)NSMutableArray *singerArray;
@property(nonatomic,strong)NSMutableArray *songNameArray;


@end

@implementation PersonalSongsViewCtl
-(NSMutableArray *)songArray{
    if (!_songArray) {
        _songArray =[NSMutableArray array];
    }
    return _songArray;
}

-(NSMutableArray *)imageArray{
    if (!_imageArray) {
        _imageArray =[NSMutableArray array];
    }
    return _imageArray;
}

-(NSMutableArray *)singerArray{
    if (!_singerArray) {
        _singerArray =[NSMutableArray array];
    }
    return _singerArray;
}
-(NSMutableArray *)songNameArray{
    if (!_songNameArray) {
        _songNameArray =[NSMutableArray array];
    }
    return _songNameArray;
}



-(UITableView *)wTableView{
    if (_wTableView==nil) {
        _wTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH , SCREEN_HEIGHT) style:UITableViewStyleGrouped];
        _wTableView.delegate = self;
        _wTableView.dataSource = self;
        _wTableView.separatorColor = [UIColor darkGrayColor];
        _wTableView.tableHeaderView = self.headerView;
        
       
        
    }
    return _wTableView;
}

-(UIView *)headerView{
    if (!_headerView) {
        _headerView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
        self.headerImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
        
      //  self.headerImage.contentMode = UIViewContentModeScaleAspectFill;
      //  self.headerImage.clipsToBounds =YES;
        [_headerView addSubview:self.headerImage];
    }
    return _headerView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.wTableView];
    
   
    [self loadData];
}

-(void)loadData{
    NSString *url = @"http://api.songlist.ttpod.com/songlists/";
    //307937293  //308570405  //
    //                http://search.dongting.com/songlist/search?q=tag:%E6%9C%80%E7%83%AD&page=1&size=10
    //   NSString *urlString = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NetworkRequest *request = [NetworkRequest shareInstance];
    [request getRequest:[NSString stringWithFormat:@"%@%@",url,self._id] parameter:nil success:^(id responseObject) {
        NSArray *songsArray = responseObject[@"songs"];
        NSString *string = responseObject[@"image"][@"pic"];
        
    [self.headerImage sd_setImageWithURL:[NSURL URLWithString:string]];
        
        for (NSInteger i = 0; i <= songsArray.count -1; i++) {
            
            if ([songsArray[i][@"urlList"] count] >=1) {
                if (![songsArray[i][@"picUrl"] isEqual:[NSNull null]]) {
                    [self.songArray addObject:[songsArray[i][@"urlList"] lastObject][@"url"]];
                    
                    [self.imageArray addObject:songsArray[i][@"picUrl"]];
                    [self.singerArray addObject:songsArray[i][@"singerName"]];
                    [self.songNameArray addObject:songsArray[i][@"name"]];
                }
            }
        }
        
        [self.wTableView reloadData];
        NSLog(@"--歌曲地址--%ld,--歌曲名--%ld,--歌手--%ld",self.songArray.count,self.songNameArray.count,self.singerArray.count);
    } failuer:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
    
}

-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.songNameArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];
   
    if (cell ==nil) {
        cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    
  //  [cell.imageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[indexPath.row]] placeholderImage:[UIImage imageNamed:@"more_icon"]];
    
    cell.textLabel.text = self.songNameArray[indexPath.row];
    cell.detailTextLabel.text = self.singerArray[indexPath.row];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.detailTextLabel.textColor = [UIColor darkGrayColor];
    cell.backgroundColor = RGBCOLOR(22, 27, 33);
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    PlayViewCtl *vc = [[PlayViewCtl alloc] init];
    vc.musicManager = [AVPlayerManager shareManager];
    vc.musicManager.listInto = YES;
    
    if (vc.musicManager.musicArray.count <=0) {
         vc.songArray = self.songArray;
    }else{
        
         [vc.musicManager removeObserver];
        
        //判断是否是同一个歌单数组 如果是 直接播放 如果不是 删除之前保存的歌单重新播放
        NSString *string = [self.singerArray lastObject];
        NSString *string1 = [vc.musicManager.singerArray lastObject];
        
       if ([string isEqualToString: string1]) {
             
       }else{
             [vc.musicManager.musicArray removeAllObjects];
             vc.songArray = self.songArray;
        }
    }
    
    vc.singerArray = self.singerArray;
    vc.imageArray = self.imageArray;
    vc.songNameArray = self.songNameArray;
    vc.index = indexPath.row;
    
    [self presentViewController:vc animated:YES completion:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
